import {
  registerMicroApps,
  addGlobalUncaughtErrorHandler,
  start,
} from "qiankun";
import apps from "./apps";
import { dispatchUserEvent } from "@/qiankun/dispatchUserEvent";

/** 注册微应用 */
registerMicroApps(apps, {
  // 微应用加载前
  beforeLoad: (app) => {
    console.log("before-load", app.name);
    return Promise.resolve();
  },
  beforeMount: (app) => {
    console.log("before mount", app);
    return Promise.resolve();
  },
  afterMount: (app) => {
    console.log("after mount", app.name);
    dispatchUserEvent();
    return Promise.resolve();
  },
});

addGlobalUncaughtErrorHandler((event: Event | string) => {
  console.error(event);
  const { message: msg } = event as any;
  if (msg && msg.includes("died in status LOADING_SOURCE_CODE")) {
    console.error("微应用加载失败，请检查应用是否可运行");
  }
});

export default start;
